package com.example.module_hp_zao_yin_media.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.Activity;
import android.graphics.Bitmap;

import android.os.Handler;
import android.os.Looper;

import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.module_hp_zao_yin_media.base.BaseMvvmActivity;
import com.example.module_hp_zao_yin_media.base.BaseViewModel;
import com.fwlst.lib_base.utils.AdUtils;
import com.fwlst.lib_base.utils.BaseUtils;
import com.example.module_hp_zao_yin_media.R;
import com.example.module_hp_zao_yin_media.databinding.ActivityHpZaoYinPhotoBinding;
import com.example.module_hp_zao_yin_media.util.AudioRecordDemo;
import com.example.module_hp_zao_yin_media.util.Utils;
import com.fwlst.lib_base.utils.JtXXPermissions;
import com.hjq.toast.Toaster;
import com.otaliastudios.cameraview.BitmapCallback;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.PictureResult;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;


@Route(path = "/hpZaoYinMedia/route/HpZaoYinPhotoActivity")
public class HpZaoYinPhotoActivity extends BaseMvvmActivity<ActivityHpZaoYinPhotoBinding, BaseViewModel> {
    CameraView camera;
    private AudioRecordDemo audioRecordDemo;
    private List<Double> numArr = new ArrayList<>();
    private Handler mHandler;

    @Override
    protected int initLayout() {
        return R.layout.activity_hp_zao_yin_photo;
    }

    @Override
    protected int initBR() {
        return 0;
    }

    @Override
    protected void initView() {
        camera = findViewById(R.id.camera);
        camera.setLifecycleOwner(this);
        //设置相机拍照事件
        camera.addCameraListener(new CameraListener() {
            @Override
            public void onPictureTaken(@NonNull PictureResult result) {
                super.onPictureTaken(result);
                if (camera.isTakingVideo()) {
                    return;
                }
                result.toBitmap(new BitmapCallback() {
                    @Override
                    public void onBitmapReady(@Nullable Bitmap bitmap) {
                        Utils.PHOTO_BM = bitmap;
                        navigateTo(HpZaoYinPhotoInfoActivity.class);
                    }
                });
            }
        });
        //点击事件-返回
        binding.hpZaoYinPhotoBt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //点击事件-拍照
        binding.hpZaoYinPhotoBt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (camera.isTakingPicture()) return;
//                AdUtils.INSTANCE.checkFunction(HpZaoYinPhotoActivity.this, new Function0<Unit>() {
//                    @Override
//                    public Unit invoke() {
                        camera.takePictureSnapshot();
//                        return null;
//                    }
//                });
            }
        });
        //点击事件-切换摄像头
        binding.hpZaoYinPhotoBt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (camera.isTakingPicture() || camera.isTakingVideo()) {
                    return;
                }
                camera.toggleFacing();
            }
        });
        //获取录音机权限
        audioRecordDemo = new AudioRecordDemo(this);
        getPermission();
        /*Timer timer = new Timer();
        timer.schedule(new TimerTask(){
            public void run(){
                setTime();
                timer.cancel();
            }
        }, 1*1000);*/
        mHandler = new Handler(Looper.getMainLooper());
        startRepeatedTask();
    }

    //开启录音权限
    private void getPermission() {
        List<String> pre = new ArrayList<>();
        pre.add(Manifest.permission.RECORD_AUDIO);
        JtXXPermissions.INSTANCE.requestPermission(HpZaoYinPhotoActivity.this, "", pre, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                audioRecordDemo.getNoiseLevel(new AudioRecordDemo.MonitorInterface() {
                    @Override
                    public void monitorListener(Double res) {
                        binding.hpZaoYinTv0.setText(res + "dB");
                        if (res > 0) {
                            numArr.add(res);
                            setNumArr();
                        }
                    }
                });
                return null;
            }
        });
    }

    //计算list最大，最小，平均值
    private void setNumArr() {
        Double maxNum = Collections.max(numArr);
        Double minNum = Collections.min(numArr);
        Double sum = 0.0;
        for (Double i : numArr) {
            sum += i;
        }
        Double avNum = sum / numArr.size();
        avNum = Math.round(avNum * 10) / 10.0;
        binding.hpZaoYinTv1.setText(maxNum + "dB");
        binding.hpZaoYinTv2.setText(avNum + "dB");
        binding.hpZaoYinTv3.setText(minNum + "dB");

    }

    private void startRepeatedTask() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // 执行任务逻辑
                setTime();
                // 继续下一次定时任务
                startRepeatedTask();
            }
        }, 1000); // 1秒后执行
    }

    //设置时间方法
    private void setTime() {
        long m = System.currentTimeMillis() / 1000;
        binding.hpZaoYinPhotoTv1.setText(BaseUtils.INSTANCE.formatTime((int) m, "yyyy.MM.dd"));
        binding.hpZaoYinPhotoTv2.setText(BaseUtils.INSTANCE.formatTime((int) m, "HH:mm"));
        binding.hpZaoYinPhotoLl.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        camera.destroy();
        audioRecordDemo.sotp();
        mHandler.removeCallbacksAndMessages(null);
    }
}